const express = require('express')
const router = express.Router()
const  {logI,logE}  = require('../../log.js')
const moment = require('moment')
const userDb = require('../db/user-db')
const sendFCM = require('../request/send-fcm');
const {retError,retSuccess} = require('../../utils/ret')
const mqtt = require('mqtt');

/*let client= mqtt.connect('mqtt://127.0.0.1:1883', {
    username: 'server',
    password: 'server',
    clientId: 'server'
});*/

router
    .post('/',  async (req, res) => {
        let {body: {mac}} = req;
        logI("C-S", mac, "NOTICE", `${JSON.stringify(req.body)}`, "null")
        try {
            if(mac){

                //const time = moment().format('YYYY-MM-DD HH:mm:ss');
                //await userDb.insertLogNotice(mac,time);
                //client.publish('camera-server-mqtt', mac.toString(), { qos: 1, retain: true });

                const fbMsg = {
                    "code":1,
                    "imgUrl":"recv.imgUrl",
                    "nickname":"recv.nickname"
                }
                await sendFCM.sendFCM(mac, fbMsg);
                retSuccess("NOTICE",0,mac,"",res);
            }else{
                retError("NOTICE",10001,"invalid params",res);
            }

        } catch (error) {
            // 处理异常
            retError("NOTICE",10001,error,res);
        }

    })
module.exports = router